import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { RepairRequest } from '../models/device.model';
import { environment } from '../../environments/environment';

@Injectable({
  providedIn: 'root'
})
export class RepairRequestService {
  private apiUrl = `${environment.apiUrl}/repairrequests`;

  constructor(private http: HttpClient) { }

  getAllRepairRequests(): Observable<RepairRequest[]> {
    return this.http.get<RepairRequest[]>(this.apiUrl);
  }

  getRepairRequest(id: number): Observable<RepairRequest> {
    return this.http.get<RepairRequest>(`${this.apiUrl}/${id}`);
  }

  createRepairRequest(request: RepairRequest): Observable<RepairRequest> {
    return this.http.post<RepairRequest>(this.apiUrl, request);
  }

  updateRepairRequest(request: RepairRequest): Observable<RepairRequest> {
    return this.http.put<RepairRequest>(`${this.apiUrl}/${request.id}`, request);
  }

  approveRepairRequest(id: number): Observable<void> {
    return this.http.post<void>(`${this.apiUrl}/${id}/approve`, {});
  }

  rejectRepairRequest(id: number, reason: string): Observable<void> {
    return this.http.post<void>(`${this.apiUrl}/${id}/reject`, reason);
  }

  deleteRepairRequest(id: number): Observable<void> {
    return this.http.delete<void>(`${this.apiUrl}/${id}`);
  }
} 